﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using Kugar.Core.BaseStruct;
using Kugar.Core.ExtMethod;

#if !NETCOREAPP
using System.Web;
using System.Web.Mvc;
using System.Web.WebPages;
    using System.Web.Mvc.Html;
using Kugar.Core.Web.ViewModel;
#endif

#if NETCOREAPP
using Microsoft.AspNetCore.Mvc.Razor;
using Microsoft.AspNetCore.Mvc.Rendering;
using Kugar.Core.Web;
#endif

namespace Kugar.MVC.MobileUI.WeUI.PageControls.PageCommons
{
    public class PageCommon_ServerMsg : ControlBase
    {
        public PageCommon_ServerMsg(ViewContext viewContext) : base(viewContext)
        {
        }

        public override MvcHtmlString Render()
        {
            var tmp = (List <VM_MVCMsgBoxItem>) ViewContext.TempData["MsgData_Temp"];

            if (tmp.HasData())
            {
                var n1 = $"scriptServerMsg_{RandomEx.Next()}";
       
                ViewContext.Writer.WriteLine($"<script id=\"{n1}\" type=\"text/javascript\">");
                
                ViewContext.Writer.WriteLine("$(document).ready(function(){");

                foreach (var item in tmp)
                {
                    var script = (item as VM_MVCMsgBoxItemEx)?.BeforceAlertScript.ToStringEx();

                    if (!string.IsNullOrWhiteSpace(item.Message))
                    {
                        ViewContext.Writer.WriteLine($"debugger;{script} WebUIJS.Alert('{item.Message}',function(index){{ ");
                    }

                    if (!string.IsNullOrWhiteSpace(item.JavsScript))
                    {
                        var n = $"severmsg_{RandomEx.Next()}";
                        ViewContext.Writer.WriteLine($"function {n} (){{");
                        ViewContext.Writer.WriteLine(item.JavsScript);
                        ViewContext.Writer.WriteLine("}");
                        ViewContext.Writer.WriteLine($"{n}.call($('#{n1}'));");
                    }

                    ViewContext.Writer.WriteLine("});");

                }
                ViewContext.Writer.WriteLine("});");


                ViewContext.Writer.WriteLine("</script>");
            }

            return MvcHtmlString.Empty;
        }
    }
}